package com.pk.platform.business.core.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.IListViewDao;
import com.pk.platform.domain.list.ListDefine;
import com.pk.platform.domain.list.ListModule;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.page.Pager;

public class ListViewDaoImpl extends GenericDaoImpl implements IListViewDao {

	public List<Map<String, Object>> queryListDefineByModule(String moduleId, Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from p_listdefine where lmId = ?");
		return this.getListByPage(sql.toString(), new Object[]{moduleId}, pager);
	}

	public List<Map<String, Object>> queryListFieldByDefine(String listId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from p_listfield where ldId = ? order by sort");
		return this.getSjt().queryForList(sql.toString(), listId);
	}

	public List<Map<String, Object>> queryListModule(Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from p_listmodule order by moduleName");
		return this.getListByPage(sql.toString(), null, pager);
	}
	
	public ListModule queryListModuleByCode(String code)
	{
		String hql = "from ListModule lm where lm.moduleCode = ?";
		List<ListModule> list = this.getHibernateTemplate().find(hql, new Object[]{code});
		if(list.size() > 0)
		{
			return list.get(0);
		}
		return null;
	}
	
	public ListDefine queryListDefineByCode(String code)
	{
		String hql = "from ListDefine ld where ld.listCode = ?";
		List<ListDefine> list = this.getHibernateTemplate().find(hql, new Object[]{code});
		if(list.size() > 0)
		{
			return list.get(0);
		}
		return null;
	}
	
	public int moduleCountByCode(String code, String id)
	{
		List<Object> param = new ArrayList<Object>();
		param.add(code);
		String sql = "select count(id) from p_listModule lm where lm.moduleCode = ?";
		if(StringConverter.isNotNull(id))
		{
			sql += " and lm.id <> ?";
			param.add(id);
		}
		int count = this.getSjt().queryForInt(sql, param.toArray());
		return count;
	}
	
	public int defineCountByCode(String code, String id)
	{
		List<Object> param = new ArrayList<Object>();
		param.add(code);
		String sql = "select count(id) from p_listDefine ld where ld.listCode = ?";
		if(StringConverter.isNotNull(id))
		{
			sql += " and ld.id <> ?";
			param.add(id);
		}
		int count = this.getSjt().queryForInt(sql, param.toArray());
		return count;
	}

}
